import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableForeignKey,
  TableIndex,
} from 'typeorm';

export class CreateFriendshipsTable1730624000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建好友关系表
    await queryRunner.createTable(
      new Table({
        name: 'friendships',
        columns: [
          {
            name: 'id',
            type: 'int',
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'userId',
            type: 'int',
            comment: '发起者ID',
          },
          {
            name: 'friendId',
            type: 'int',
            comment: '好友ID',
          },
          {
            name: 'status',
            type: 'enum',
            enum: ['pending', 'accepted', 'rejected', 'blocked'],
            default: "'pending'",
            comment: '好友状态',
          },
          {
            name: 'remark',
            type: 'text',
            isNullable: true,
            comment: '申请备注',
          },
          {
            name: 'createdAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            comment: '创建时间',
          },
          {
            name: 'updatedAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            onUpdate: 'CURRENT_TIMESTAMP',
            comment: '更新时间',
          },
        ],
      }),
      true,
    );

    // 添加外键
    await queryRunner.createForeignKey(
      'friendships',
      new TableForeignKey({
        columnNames: ['userId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    await queryRunner.createForeignKey(
      'friendships',
      new TableForeignKey({
        columnNames: ['friendId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    // 创建唯一索引（防止重复添加）
    await queryRunner.createIndex(
      'friendships',
      new TableIndex({
        name: 'IDX_friendships_userId_friendId',
        columnNames: ['userId', 'friendId'],
        isUnique: true,
      }),
    );

    // 创建查询索引
    await queryRunner.query(
      'CREATE INDEX `IDX_friendships_userId` ON `friendships` (`userId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_friendships_friendId` ON `friendships` (`friendId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_friendships_status` ON `friendships` (`status`)',
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 删除索引
    await queryRunner.query(
      'DROP INDEX `IDX_friendships_status` ON `friendships`',
    );
    await queryRunner.query(
      'DROP INDEX `IDX_friendships_friendId` ON `friendships`',
    );
    await queryRunner.query(
      'DROP INDEX `IDX_friendships_userId` ON `friendships`',
    );
    await queryRunner.dropIndex(
      'friendships',
      'IDX_friendships_userId_friendId',
    );

    // 删除表
    await queryRunner.dropTable('friendships');
  }
}
